<template>
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :http-request="uploadFn"
      :file-list="fileList"
      :limit="6"
      :on-remove="removeFn"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-show="isShow" :text-inside="true" :stroke-width="18" :percentage="percent" status="success" />
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID50g6GAn9E2ZpoWyA6MPChvsZz8zauwhD',
  SecretKey: 'GExQcXBXeU3KL5YlhKgkX3IFvKSIr9Bv'
})

export default {
  
  data() {
    return {
      fileList: [
        // {
        //   url: this.staffPhoto
        // }
      ],
      percent: 0,
      isShow: false
    }
  },
  methods: {
    // 移除文件后触发
    removeFn(d) {
      console.log(this.fileList);
      this.fileList = this.fileList.filter(item => item.uid != d.uid)
      // console.log(d);
    },
    uploadFn(file) {
      this.isShow = true
      console.log('file', file)
      // this.fileList.push({
      //   url: ''
      // })
      cos.putObject({
        Bucket: 'hr-project-75-1253450230', /* 必须 存储桶名字 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: file.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: file.file, // 上传文件对象
        onProgress: (progressData) => { // 上传进度
          console.log(JSON.stringify(progressData))
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        console.log(err || data)
        this.fileList.push({url:'http://' + data.Location})
        // this.fileList.map(item => item = item.url)
        console.log(this.fileList);
        setTimeout(() => {
          this.isShow = false
        }, 1000)
        
        // this.$emit('update:staffPhoto', this.fileList)
      })
    }
  }
}
</script>

<style lang='scss' scoped>
 .disable{
   .el-upload--picture-card{
     display: none;
   }
 }
 .el-progress{
   position: absolute;
   bottom: -10px;
  width: 80%;
 }

</style>
